from flask import Blueprint,jsonify

from db import Dd
from tool import rs
import json
bp_buli_list = Blueprint('huli_list',__name__)

@bp_buli_list.route('/getlist')
def getlist():
    sql = 'select * from holiday_type'
    db = Dd()
    
    res = db.get_all(sql)
    return jsonify({'code':200,'list':res})

@bp_buli_list.route('/getresourcedata')
def getresourcedata():
    #从redis取，如果有直接返回
    db = Dd()
    
    values = rs.str_get('dataresouce')
    if values:
        list = eval(values)
    else:
        #如果没有查询
        sql = "select * from data_source"
        res = db.get_all(sql)
        list = []
        if res:
            for i in res:
                dict={'id':i['id'],'name':i['name']}
                sql1 = "select id,name,type from %s"%(i['tablename'])
                subres = db.get_all(sql1)
                dict['mes'] = subres
                list.append(dict)
        #存入redis
        rs.str_set('dataresouce',json.dumps(list))
    return jsonify({"code":200,'list':list})